package com.livedocumentation.client.webservices.project;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.livedocumentation.client.GWTEntryPoint;
import com.livedocumentation.client.web.header.Header;
import com.livedocumentation.client.web.header.ProjectName;
import com.livedocumentation.client.webservices.ConvertJsonp;
import com.livedocumentation.client.webservices.RequestSubversionSpecificationFile;

public class RequestGetProjectInfo {

	private static String url = "http://jsonpfy.projectnection.appspot.com/GetDataService";

	public static void getJsonpRequest(final String projectID,
			final String classPath) {

		JsonpRequestBuilder jsonp = new JsonpRequestBuilder();

		jsonp.setTimeout(10000);

		String urlGet = url + "?kind=Entity&ID=" + projectID;

		jsonp.requestObject(urlGet, new AsyncCallback<JavaScriptObject>() {

			public void onFailure(Throwable throwable) {

				System.out.println("Error: " + throwable);
			}

			public void onSuccess(JavaScriptObject feed) {

				JSONObject obj = new JSONObject(feed);

				String ID = ConvertJsonp.convertToString(obj.get("ID"));

				if (ID != null) {

					GWTEntryPoint.currentProject = ConvertJsonpToProject
							.convertToProjectInfo(obj,
									GWTEntryPoint.currentProject);
				}

				Header.vpProjectName.clear();
				Header.vpProjectName.add(new ProjectName(
						GWTEntryPoint.currentProject));

				RequestSubversionSpecificationFile.loadJSONFile(classPath);
			}
		});
	}
}
